package com.meituan.catering.management.shop.biz.validator;

import com.meituan.catering.management.common.exception.BizException;
import com.meituan.catering.management.common.model.api.http.ContactHttpModel;
import com.meituan.catering.management.common.model.enumeration.ErrorCode;
import com.meituan.catering.management.shop.api.http.model.request.*;
import com.meituan.catering.management.shop.dao.mapper.ShopMapper;
import com.meituan.catering.management.shop.dao.model.ShopDO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author mac
 */
@Component
public class ShopBizServiceValidator {

    @Resource
    private ShopMapper shopMapper;

    public void createValid(Long tenantId, Long userId, CreateShopHttpRequest createShopHttpRequest) throws BizException {
        paramValid(tenantId, userId, createShopHttpRequest);
        contactValid(createShopHttpRequest.getContact());
    }

    public void findByBusinessNo(Long tenantId, Long userId, String businessNo) throws BizException {
        paramValid(tenantId, userId, businessNo);
    }

    public void searchShopValid(Long tenantId, Long userId, SearchShopHttpRequest searchShopHttpRequest) throws BizException {
        paramValid(tenantId, userId, searchShopHttpRequest);
    }

    public void updateValid(Long tenantId, Long userId, String businessNo, UpdateShopHttpRequest updateShopHttpRequest) throws BizException {
        paramValid(tenantId, userId, updateShopHttpRequest);
        paramValid(tenantId, userId, businessNo, updateShopHttpRequest, updateShopHttpRequest.getVersion());
        contactValid(updateShopHttpRequest.getContact());
    }

    public void openValid(Long tenantId, Long userId, String businessNo, OpenShopHttpRequest openShopHttpRequest) throws BizException {
        paramValid(tenantId, userId, openShopHttpRequest);
        paramValid(tenantId, userId, businessNo, openShopHttpRequest, openShopHttpRequest.getVersion());
    }

    public void closeValid(Long tenantId, Long userId, String businessNo, CloseShopHttpRequest closeShopHttpRequest) throws BizException {
        paramValid(tenantId, userId, closeShopHttpRequest);
        paramValid(tenantId, userId, businessNo, closeShopHttpRequest, closeShopHttpRequest.getVersion());
    }

    private void paramValid(Long tenantId, Long userId, Object request) throws BizException {
        if (tenantId < 0 || userId < 0 || request == null) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
    }

    public void paramValid(Long tenantId, Long userId, String businessNo, Object request, Integer version) throws BizException {
        if (tenantId < 0 || userId < 0 || request == null) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
        ShopDO shopDO = shopMapper.findByBusinessNo(tenantId, userId, businessNo);
        if (shopDO == null) {
            throw new BizException(ErrorCode.SYSTEM_ERROR);
        }
        if (!shopDO.getVersion().equals(version)) {
            throw new BizException(ErrorCode.SYSTEM_ERROR);
        }
    }

    public void contactValid(ContactHttpModel contactHttpModel) throws BizException {
        String cellphoneRegex = "0?(13|14|15|17|18|19)[0-9]{9}";
        String telephoneRegex = "[0-9-()（）]{7,18}";
        String nameRegex = "[\u4e00-\u9fa5]{2,5}";
        String addressRegex = "[\u4e00-\u9fa5]{1,50}";

        Pattern cellphone = Pattern.compile(cellphoneRegex);
        Pattern telephone = Pattern.compile(telephoneRegex);
        Pattern name = Pattern.compile(nameRegex);
        Pattern address = Pattern.compile(addressRegex);

        Matcher cellphoneMatcher = cellphone.matcher(contactHttpModel.getCellphone());
        Matcher telephoneMatcher = telephone.matcher(contactHttpModel.getTelephone());
        Matcher nameMatcher = name.matcher(contactHttpModel.getName());
        Matcher addressMatcher = address.matcher(contactHttpModel.getAddress());

        if (!cellphoneMatcher.matches()) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
        if (!telephoneMatcher.matches()) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
        if (!nameMatcher.matches()) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
        if (!addressMatcher.matches()) {
            throw new BizException(ErrorCode.PARAM_ERROR);
        }
    }
}
